package com.zmx.product.web;

import com.zmx.common.common.web.AbstractController;
import com.zmx.common.common.web.rest.ResponseResult;
import com.zmx.product.dto.ProductCategoryDto;
import com.zmx.product.dto.result.IndexCategoryDTO;
import com.zmx.product.service.ProductCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description: 商品分类控制器
 * @ClassName: ProductCategoryController
 * @Author zhaomxr
 * @Date 2022-01-11 13:56
 */
@Api("商品分类")
@Slf4j
@RestController
@RequestMapping("category")
public class ProductCategoryController extends AbstractController<ProductCategoryDto, ProductCategoryService> {

    @Autowired
    private ProductCategoryService productCategoryService;

    @ApiOperation(value = "获取所有一级分类", tags = "获取所有一级分类")
    @GetMapping(value = "getCategory")
    public Object getCategory() {
        try {
            List<ProductCategoryDto> categoryDtos = productCategoryService.getCategory();
            return ResponseResult.success(categoryDtos);
        } catch (Exception e) {
            log.error("获取所有一级分类失败！", e);
            return ResponseResult.fail("获取所有一级分类失败");
        }
    }

    @ApiOperation(value = "只取排名前四个分类", tags = "只取排名前四个分类")
    @GetMapping(value = "getCategoryOnly4")
    public Object getCategoryOnly4() {
        try {
            List<IndexCategoryDTO> indexCategoryDTOList = productCategoryService.getCategoryOnly4();
            return ResponseResult.success(indexCategoryDTOList);
        } catch (Exception e) {
            log.error("只取排名前四个分类失败！", e);
            return ResponseResult.fail("只取排名前四个分类失败");
        }
    }
}
